//
//  FillinInfoViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/24.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "FillinInfoViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"

#import "BillConfirmViewController.h"

@interface FillinInfoViewController ()
{
    NSInteger boughtCount;
}
@end

@implementation FillinInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写信息";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //start&stepper
    self.startTime.text = self.startStr;
    self.priceLabel.text = [NSString stringWithFormat:@"%ld元/次",(long)self.price];
    
    boughtCount = 1;
    self.stepperView.clipsToBounds = YES;
    self.stepperView.layer.borderWidth = 1;
    self.stepperView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:0.5].CGColor;
    [self.stepperView.layer setCornerRadius:5];
    self.stepperMinus.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.stepperMinus.layer.borderWidth = 0.5;
    self.stepperMinus.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:0.5].CGColor;
    [self.stepperMinus addTarget:self action:@selector(minus) forControlEvents:UIControlEventTouchUpInside];
    self.stepperPlus.backgroundColor = [UIColor colorWithHexString:AppColor_Main];
    self.stepperPlus.layer.borderWidth = 0.5;
    self.stepperPlus.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:0.5].CGColor;
    [self.stepperPlus addTarget:self action:@selector(plus) forControlEvents:UIControlEventTouchUpInside];
    
    
    //contactsView
    self.contactsView.clipsToBounds = YES;
    [self.contactsView.layer setCornerRadius:5];
    self.contactsView.layer.borderWidth = 1;
    self.contactsView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:0.5].CGColor;
    self.contactsSeperate1.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    self.contactsSeperate2.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    self.nameText.delegate = self;
    self.telText.delegate = self;
    self.mailText.delegate = self;


    //toolBar
    self.expense.text = [NSString stringWithFormat:@"￥ %ld",(long)self.price];
    [self.billConfirm.layer setCornerRadius:5];
    [self.billConfirm addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - minus&plus
- (void)minus
{
    if (boughtCount > 1) {
        boughtCount = boughtCount - 1;
        self.buyCount.text = [NSString stringWithFormat:@"%ld",(long)boughtCount];
    } else {}
    
    if (boughtCount > 1) {
        self.stepperMinus.backgroundColor = [UIColor colorWithHexString:AppColor_Main];
        [self.stepperMinus setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        self.stepperMinus.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
        [self.stepperMinus setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }
    
    self.expense.text = [NSString stringWithFormat:@"￥ %ld",(long)self.price*boughtCount];
}
- (void)plus
{
    boughtCount = boughtCount + 1;
    self.buyCount.text = [NSString stringWithFormat:@"%ld",(long)boughtCount];
    
    if (boughtCount > 1) {
        self.stepperMinus.backgroundColor = [UIColor colorWithHexString:AppColor_Main];
        [self.stepperMinus setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {}
    
    self.expense.text = [NSString stringWithFormat:@"￥ %ld",(long)self.price*boughtCount];
}
#pragma mark - minus&plus End

#pragma mark - tap
- (void)tap
{
    [self.nameText resignFirstResponder];
    [self.telText resignFirstResponder];
    [self.mailText resignFirstResponder];
}
#pragma mark - tap End

#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.nameText) {
        [self.telText becomeFirstResponder];
    } else if (textField == self.telText) {
        [self.mailText becomeFirstResponder];
    } else if (textField == self.mailText) {
        [self.mailText resignFirstResponder];
    }
    return YES;
}
#pragma mark - textField delegate End

#pragma mark -billConfirm
- (void)confirm
{
    BillConfirmViewController *billConfirmVC = [[BillConfirmViewController alloc] init];
    billConfirmVC.productID = self.productID;
    billConfirmVC.name = self.productName;
    billConfirmVC.expense = self.expense.text;
    billConfirmVC.startStr = self.startStr;
    billConfirmVC.boughtCount = boughtCount;
    [self.navigationController pushViewController:billConfirmVC animated:YES];
}
#pragma mark -billConfirm

@end
